package com.ruirenyun.util.annotation.validator;


import org.apache.commons.lang3.StringUtils;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

/**
 * Created with IntelliJ IDEA.
 *
 * @author Deng Ye
 * @version 1.0
 * @date 2020/9/11
 * @description
 * @modifiedBy
 */
public class LengthValidator implements ConstraintValidator<Length, String> {
    private int max = Integer.MAX_VALUE;
    private int min = 0;

    @Override
    public void initialize(Length length) {
        max = length.max();
        min = length.min();
    }

    @Override
    public boolean isValid(String value, ConstraintValidatorContext context) {
        int len = StringUtils.isEmpty(value) ? 0:value.length();
        return len >= min && len <= max;
    }
}
